package com.type;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Photo {
	private int photoWidth;
	private int photoHeight;
	private byte[] photoByteArray;

	
	public Photo() {
		
	}
	
	public byte[] getPhotoByteArray() {
		return photoByteArray;
	}
	
	
	public void receive(DataInputStream dis) throws IOException, InterruptedException {
		photoWidth = dis.readInt();
		photoHeight = dis.readInt();
		photoByteArray = new byte[((photoWidth * photoHeight) << 2)];
	
		int currentIndex = 0;
		int storeByteNumber = 0;
		while (currentIndex != photoByteArray.length) {
			storeByteNumber = photoByteArray.length - currentIndex >= 512 ? 512 : photoByteArray.length - currentIndex;
			byte tempArray[] = new byte[storeByteNumber];
			dis.read(tempArray, 0, storeByteNumber);
			System.arraycopy(tempArray, 0, photoByteArray, currentIndex, storeByteNumber);
			currentIndex += storeByteNumber;
		}
		
		Thread.sleep(150);
	}

	
	public void send(DataOutputStream dos) throws IOException, InterruptedException {
		dos.writeInt(photoWidth);
		dos.writeInt(photoHeight);
	
		int currentIndex = 0;
		int storeByteNumber = 0;
		while (currentIndex != photoByteArray.length) {
			storeByteNumber = photoByteArray.length - currentIndex >= 512 ? 512 : photoByteArray.length - currentIndex;
			byte tempArray[] = new byte[storeByteNumber];
			System.arraycopy(photoByteArray, currentIndex, tempArray, 0, storeByteNumber);
			currentIndex += storeByteNumber;
			dos.write(tempArray, 0, storeByteNumber);
		}
		
		Thread.sleep(150);
	}
}
